﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ebclang.basics
{
    public static class RequestExtensions
    {
        public static RequestContinuation<TResponse> Request<TRequest, TResponse>(this MulticastDelegate theEvent, TRequest requestData)
        {
            return new RequestContinuation<TResponse>(processResult => theEvent.DynamicInvoke(new Request<TRequest, TResponse>(requestData, processResult)));
        }


        public class RequestContinuation<TResponse>
        {
            private readonly Action<Action<TResponse>> eventWrapper;

            public RequestContinuation(Action<Action<TResponse>> eventWrapper)
            {
                this.eventWrapper = eventWrapper;
            }


            public void Receive(Action<TResponse> processResult)
            {
                eventWrapper(processResult);
            }


            public TResponse Return()
            {
                TResponse response = default(TResponse);
                eventWrapper(r => response = r);
                return response;
            }
        }
    }
}
